﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.ComponentModel;
using System.Data.SqlTypes;

using Infrastructure.NetExtensions;
using TempEmployees.BLL.Validations;

namespace TempEmployees.Web
{
    public partial class DisplayTextBox : BaseDisplayTextBox
    {
        public EventHandler OnTextChanged
        {
            set { DispTextBox.TextChanged += new EventHandler(value); }
        }

        public override bool ReadOnly
        {
            set
            {
                DispLabel.Visible = value;
                DispTextBox.Visible = !value;
                if (value) DispLabel.Text = (EnableCalender) ? DispTextBox_CalendarExtender.SelectedDate.Value.IsraelFormat() : DispTextBox.Text;;
            }
            get
            {
                return DispLabel.Visible;
            }
        }

        [CategoryAttribute("Appearance")]
        public override string Text
        {
            set
            {
                DispTextBox.Text = value;
                DispLabel.Text = value;
            }
            get
            {
                return DispTextBox.Text;
            }
        }

        [CategoryAttribute("Appearance"),
         DescriptionAttribute("Extran info shown next to the text box"),
         DefaultValueAttribute("")]
        public string ExtraInfo
        {
            get { return LabelExtraInfo.Text; }
            set { LabelExtraInfo.Text = value; }
        }

        [CategoryAttribute("Behavior"),
         DescriptionAttribute("Enable the calender extender"),
         DefaultValueAttribute(false)]
        public bool EnableCalender
        {
            get { return DispTextBox_CalendarExtender.EnabledOnClient; }
            set { DispTextBox_CalendarExtender.EnabledOnClient = value; }
        }

        public override bool AutoPostBack
        {
            get { return DispTextBox.AutoPostBack; }
            set { DispTextBox.AutoPostBack = value; }
        }

        public DateTime? SelectedDate
        {
            get { return DispTextBox_CalendarExtender.SelectedDate; }
            set { DispTextBox_CalendarExtender.SelectedDate = value; }
        }

        protected override WebControl EditableControl
        {
            get { return DispTextBox; }
        }

        public bool GetSelectedDate(out DateTime date)
        {
            bool result = DateTime.TryParse(DispTextBox.Text, out date);
            if (date == DateTime.MinValue) date = (DateTime)SqlDateTime.MinValue;
            return result;
        }

        public override void Reset()
        {
            DispTextBox.Text = string.Empty;
            DispLabel.Text = string.Empty;
            SelectedDate = null;
        }

        public override bool IsFilled()
        {
            return (DispTextBox.Text.Length > 0);
        }

        public override bool IsValid()
        {
            return ValidationMethodFactory.GetValidationMethod(ValidationType)(DispTextBox.Text);
        }
    }
}